package com.example.gokchinesefoodmappcdev.controller;

import com.example.gokchinesefoodmappcdev.entity.CustomerInfo;
import com.example.gokchinesefoodmappcdev.entity.MerchantInfo;
import com.example.gokchinesefoodmappcdev.entity.PageBean;
import com.example.gokchinesefoodmappcdev.service.MerchantInfoService;
import com.example.gokchinesefoodmappcdev.util.ResultData;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;

@RestController
@RequestMapping("/rest/merchant")
public class MerchantController {
    @Autowired
    private MerchantInfoService merchantInfoService;

    /**
     * 添加商户
     * @param merchantInfo 商户信息
     * @return
     */
    @RequestMapping("/save")
    public ResultData insertMerchantInfo(MerchantInfo merchantInfo) {
        ResultData resultData = merchantInfoService.insertMerchantInfo(merchantInfo);
        return resultData;
    }

    /**
     * 分页查询
     * 根据条件查询所有用户
     * @param merchantInfo
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/list")
    public PageBean<MerchantInfo> findMerchantInfoAll(MerchantInfo merchantInfo, int pageNum, int pageSize) {
        PageBean<MerchantInfo> merchantInfoAll = merchantInfoService.findMerchantInfoAll(merchantInfo, pageNum, pageSize);
        return merchantInfoAll;
    }

    /**
     * 根据id查询商户
     * @param id 商户id
     * @return
     */
    @RequestMapping("/info")
    public ResultData findMerchantInfoById(Integer id) {
        ResultData resultData = merchantInfoService.findMerchantInfoById(id);
        return resultData;
    }

    /**
     * 更新商户
     * @param merchantInfo
     * @return
     */
    @RequestMapping("/update")
    public ResultData updateMerchantInfoById(MerchantInfo merchantInfo) {
        ResultData resultData = merchantInfoService.updateMerchantInfoById(merchantInfo);
        return resultData;
    }

    /**
     * 批量删除商户
     * @param ids
     * @return
     */
    @RequestMapping("/remove")
    public ResultData deleteMerchanInfoByIds(Integer[] ids) {
        ResultData resultData = merchantInfoService.deleteMerchanInfoByIds(ids);
        return resultData;
    }

    /**
     * 修改商户状态
     * @param merchantInfo
     * @return
     */
    @RequestMapping("/updateStatus")
    public ResultData updateMerchanInfoStatusById(MerchantInfo merchantInfo){
        ResultData resultData = merchantInfoService.updateMerchanInfoStatusById(merchantInfo);
        return resultData;
    }

    /**
     * 商户导出
     * @param merchantInfo
     */
    @RequestMapping("/listToExcel")
    public void MerchanInfoListToExcel(MerchantInfo merchantInfo,HttpServletResponse response) {
        //创建HSSFWorkbook对象,  excel的文档对象
        HSSFWorkbook workbook = new HSSFWorkbook();
        //excel的表单
        HSSFSheet sheet = workbook.createSheet("商户列表");

        List<MerchantInfo> merchantInfoList = merchantInfoService.MerchanInfoListToExcel(merchantInfo);
        //设置要导出的文件的名字
        String fileName = "merchantInfo"+".xls";
        //新增数据行，并且设置单元格数据
        int rowNum = 1;
        String[] headers = { "商户编号", "商户名称", "商户管理帐号", "商户类型","创建时间","状态"};
        //headers表示excel表中第一行的表头
        HSSFRow row = sheet.createRow(0);
        //在excel表中添加表头
        for(int i=0;i<headers.length;i++){
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);
        }
        // 时间格式化
        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        //在表中存放查询到的数据放入对应的列
        for (MerchantInfo merchantInfo1 : merchantInfoList) {
            HSSFRow row1 = sheet.createRow(rowNum);
            row1.createCell(0).setCellValue(merchantInfo1.getMerchantCode());
            row1.createCell(1).setCellValue(merchantInfo1.getMerchantName());
            row1.createCell(2).setCellValue(merchantInfo1.getUserName());
            row1.createCell(3).setCellValue(merchantInfo1.getMerchantType());
            row1.createCell(4).setCellValue(sdf.format(merchantInfo1.getGmtCreate()));
            row1.createCell(5).setCellValue(merchantInfo1.getStatus());
            rowNum++;
        }
        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        try {
            response.flushBuffer();
            workbook.write(response.getOutputStream());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
